package com.walker.taogong.web;

import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.walker.taogong.bll.UserBll;
import com.walker.taogong.datamodel.Sex;
import com.walker.taogong.datamodel.User;
import com.walker.taogong.datamodel.Worker;

public class ApplyWorker
{

    /** Logger. */
    private static Logger log = LoggerFactory.getLogger(ApplyWorker.class);

    /************************ BLL 服务引用 *******************************************/

    private UserBll userBll;

    public void setUserBll(UserBll userBll)
    {
        this.userBll = userBll;
    }

    /************************* 启动测试用 ************************************/
    /**
     * 组件启动初期化
     */
    protected void activate(ComponentContext cc) throws Exception
    {
        log.info("{} is starting!", cc.getProperties().get("name"));

        try
        {
            // 主流程

            User user = userBll.getUserByEmail("lisi@test.com");
            Worker worker = new Worker(user.getId());
            
            worker.setTruelyName("李四");
            worker.setSex(Sex.Male);

            if (userBll.createWorker(worker))
            {
                log.info("你已经成功注册完成:" + user.getUserName());
            }
            else
            {
                log.info("用户已经存在！");
            }

            if (userBll.login(user))
            {
                log.info(user.getUserName() + "已经成功登陆");
            }
            else
            {
                log.info("输入的用户信息不正，请重新输入");
            }
        }
        catch (Throwable exc)
        {
            log.error("Terminated with runtime exception", exc);
        }

    }

}
